﻿#region License


// Virtual Essentials - http://www.Virtual-Essentials.com
// Copyright (c) 2005-2013
// 
//  This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using DotNetNuke.Framework;
using DotNetNuke.Web.Client.ClientResourceManagement;
using VirtualEssentials.DNN.Modules.Testimonials.Controller;

namespace VirtualEssentials.DNN.Modules.Testimonials
{
    /// <summary>
    /// Summary description for Testimonial
    /// </summary>
    public partial class AddTestimonial : DotNetNuke.Entities.Modules.PortalModuleBase
    {
        DataProvider data = new DataProvider();
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //Controller.TestimonialController objController = new Controller.TestimonialController();
            try
            {
                //ensure website is formatted properly, if it is being submitted
                if (!string.IsNullOrEmpty(txtWebsite.Text))
                {
                    if (! (txtWebsite.Text.StartsWith("http://") || txtWebsite.Text.StartsWith("https://"))){
                        txtWebsite.Text = "http://" + txtWebsite.Text;
                    }
                }

                //8/14/2013 [briana] requested by Work Item#1435
                ModuleSecurity ms = new ModuleSecurity(this.ModuleConfiguration);
                if (ms.HasPublishTestimonialPermission)
                {
                    //if the user adding the testimonial has publish testimonial permissions, then directly publish to the system
                    TestimonialInfo t = new TestimonialInfo();
                    t.Testimonial = txtTestimonial.Text;
                    t.AuthorFirstName = txtFirstName.Text;
                    t.AuthorLastName = txtLastName.Text;
                    t.AuthorLocation = txtLocation.Text;
                    t.AuthorTitle = txtTitle.Text;
                    t.AuthorWebsite = txtWebsite.Text;
                    t.IsPublished = true;
                    t.TestimonialDate = DateTime.Now.ToString();
                    t.ModuleId = this.ModuleId;
                    data.AddNewTestimonial(t);
                   // objController.AddNewTestimonial(txtTestimonial.Text, txtFirstName.Text, txtLastName.Text, txtLocation.Text, this.ModuleId, txtTitle.Text, txtWebsite.Text, true);
                }
                else
                {
                     TestimonialInfo t = new TestimonialInfo();
                    t.Testimonial = txtTestimonial.Text;
                    t.AuthorFirstName = txtFirstName.Text;
                    t.AuthorLastName = txtLastName.Text;
                    t.AuthorLocation = txtLocation.Text;
                    t.AuthorTitle = txtTitle.Text;
                    t.AuthorWebsite = txtWebsite.Text;
                    t.IsPublished = false;
                    t.TestimonialDate = DateTime.Now.ToString();
                    t.ModuleId = this.ModuleId;
                    data.AddNewTestimonial(t);
                    //objController.AddNewTestimonial(txtTestimonial.Text, txtFirstName.Text, txtLastName.Text, txtLocation.Text, this.ModuleId, txtTitle.Text, txtWebsite.Text, false);
                }
                //redirect the page to close the popup
                Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(), true);
            }
            catch (DotNetNuke.Services.Exceptions.ModuleLoadException ex)
            {
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this.Page, ex);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                //remove any need for validation                
                btnCancel.CausesValidation = false;
                //redirect the page to close the popup
                Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(), true);
            }
            catch (DotNetNuke.Services.Exceptions.ModuleLoadException ex)
            {
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this.Page, ex);
            }
        }

    }
}